package lj.courseinterect.rest.resources;

import lj.courseinterect.dao.i.ExamItemDao;
import lj.courseinterect.dao.i.StudentItemDao;
import lj.courseinterect.entity.ExamItem;
import lj.courseinterect.entity.StudentItem;
import lj.courseinterect.transfer.OperationTransfer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;


@Component
@Path("/student_item")
public class StudentItemResource
{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private StudentItemDao studentItemDao;

    public Logger getLogger() {
        return logger;
    }

    public StudentItemDao getStudentItemDao() {
        return studentItemDao;
    }

    public void setStudentItemDao(StudentItemDao studentItemDao) {
        this.studentItemDao = studentItemDao;
    }

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public OperationTransfer create(StudentItem studentItem)
    {
        this.logger.info("create(): " + studentItem.getStuAns());
        OperationTransfer operationTransfer;

        try {
            this.studentItemDao.save(studentItem);
            operationTransfer = new OperationTransfer(true, "回答成功");
        } catch (Exception ex){
            operationTransfer = new OperationTransfer(false, "回答失败");
        }

        this.logger.info("create(): " + operationTransfer.toString());
        return operationTransfer;
    }


}